package com.xmbandu.app.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xmbandu.app.R;
import com.xmbandu.app.base.BaseActivity;

public class SspCommonWebActivity extends BaseActivity {

    private static final String URL = "URL";
    private static final String COLOR_STR = "COLOR";

    private ImageView ssp_web_back;
    private TextView ssp_web_title;
    private ProgressBar ssp_web_pb;
    private WebView ssp_web_wv;
    private RelativeLayout ssp_web_top;

    private String url;

    public static void intoSspCommonWeb(Context context, String url) {
        Intent intent = new Intent(context, SspCommonWebActivity.class);
        intent.putExtra(URL, url);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    public static void intoSspCommonWeb(Context context, String url, String color) {
        Intent intent = new Intent(context, SspCommonWebActivity.class);
        intent.putExtra(URL, url);
        intent.putExtra(COLOR_STR, color);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    public static void intoSspCommonWeb(Context context, String url, String color, boolean isHaveTop) {
        Intent intent = new Intent(context, SspCommonWebActivity.class);
        intent.putExtra(URL, url);
        intent.putExtra(COLOR_STR, color);
        intent.putExtra("isHaveTop", isHaveTop);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_web;
    }

    @Override
    public void initViews() {

        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);

        ssp_web_back = $(R.id.ssp_web_back);
        ssp_web_wv = $(R.id.ssp_web_wv);
        ssp_web_title = $(R.id.ssp_web_title);
        ssp_web_pb = $(R.id.ssp_web_pb);
        ssp_web_top = $(R.id.ssp_web_top);

    }

    @Override
    public void initListener() {
        setOnClick(ssp_web_back);
    }

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    public void initData() {

        boolean isTop = getIntent().getBooleanExtra("isHaveTop", true);

        if (isTop)
            ssp_web_top.setVisibility(View.VISIBLE);
        else
            ssp_web_top.setVisibility(View.GONE);

        url = getIntent().getStringExtra(URL);
        String colorStr = getIntent().getStringExtra(COLOR_STR);
        if (!TextUtils.isEmpty(colorStr)) {
            try {
                if ("#ffffff".equals(colorStr)) {
                    ssp_web_back.setBackgroundResource(R.mipmap.ssp_icon_left_back2);
                    ssp_web_title.setTextColor(Color.parseColor("#333333"));

                }
                int color = Color.parseColor(colorStr);
                ssp_web_top.setBackgroundColor(color);
            } catch (Exception e) {

            }
        }
        ssp_web_wv.getSettings().setJavaScriptEnabled(true);
        ssp_web_wv.getSettings().setAllowUniversalAccessFromFileURLs(true);
        ssp_web_wv.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return super.shouldOverrideUrlLoading(view, url);
            }
        });
        ssp_web_title.setText("万图拍隐私政策与使用条款");
        ssp_web_wv.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress == 100) {
                    ssp_web_pb.setVisibility(View.GONE);//加载完网页进度条消失
                } else {
                    ssp_web_pb.setVisibility(View.VISIBLE);//开始加载网页时显示进度条
                    ssp_web_pb.setProgress(newProgress);//设置进度值
                }

            }
        });
        ssp_web_wv.loadUrl(url);
    }

    @Override
    public void processClick(View v) {
        int i = v.getId();
        if (i == R.id.ssp_web_back) {
            if (ssp_web_wv.canGoBack()) {
                ssp_web_wv.goBack();
            } else {
                finish();
            }
        }
    }

}
